package com.demo.exception;

import com.demo.common.ErrorDetail;
import lombok.Getter;

@Getter
public class ApiException extends Exception {
    private final String errorCode;
    private final String message;
    private final ErrorDetail errorDetail;

    // 构造器1：基础版
    public ApiException(String errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
        this.message = message;
        this.errorDetail = ErrorDetail.builder()
                .httpCode(500)
                .message(message)
                .build();
    }

    // 构造器2：增强版
    public ApiException(ErrorDetail detail) {
        super(detail.getMessage());
        this.errorCode = String.valueOf(detail.getHttpCode());
        this.message = detail.getMessage();
        this.errorDetail = detail;
    }

    // 构造器3：带原始异常
    public ApiException(ErrorDetail detail, Throwable cause) {
        super(detail.getMessage(), cause);
        this.errorCode = String.valueOf(detail.getHttpCode());
        this.message = detail.getMessage();
        this.errorDetail = detail;
    }

    public ApiException(String errorCode, Throwable cause) {
        super(cause.getMessage(), cause);
        this.errorCode = errorCode;
        this.message = cause.getMessage();
        this.errorDetail = ErrorDetail.builder()
                .httpCode(500)
                .message(cause.getMessage())
                .build();
    }

    // 在 com.demo.exception.ApiException 类中添加：
    public ApiException(String errorCode, String message, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
        this.message = message;
        this.errorDetail = ErrorDetail.builder()
                .httpCode(500)
                .message(message)
                .reason(cause.getMessage())
                .build();
    }
}